﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mscook.core.domain;
using NHibernate.Linq;
namespace mscook.core.repositories
{
   public class BulletinRepository:Repository<Bulletin>, mscook.core.repositories.IBulletinRepository
    {
       /*
       public Dictionary<string,IList<Bulletin>> GetGroupByType()
       {
           var query = (from x in this.Session.Query<Bulletin>() select x)
             .Where(o => o.IsDelete==false)
            ;
           return query.ToList().ToDictionary(o=>o.Type);
       }
        * */

       public IList<Bulletin> GetByType(string type, int start, int limit)
       {
           var query = (from x in this.Session.Query<Bulletin>() select x)
            .Where(o => o.IsDelete == false
                && o.Type == type).OrderByDescending(o=>o.EntityIndex).OrderByDescending(o=>o.CreateTime);
           return query.Skip(start).Take(limit).ToList();
       }

       public IList<Bulletin> GetByExit(IList<string> exitIds, int start, int limit,out long total)
       {
           var query = (from x in this.Session.Query<Bulletin>() select x)
            .Where(o => o.IsDelete == false
                && (!exitIds.Contains(o.Id))
                ).OrderByDescending(o=>o.EntityIndex).OrderByDescending(o => o.CreateTime);
           total = query.Count();
           return query.Skip(start).Take(limit).ToList();
       }

       public IList<Bulletin> SelectPageList(int start, int limit, out long total, string title)
       {
           var query = (from x in this.Session.Query<Bulletin>() select x)
              .Where(o => o.IsDelete == false
                  && (string.IsNullOrEmpty(title) || o.Title.Contains(title))
              ).OrderByDescending(o => o.EntityIndex)
              .OrderByDescending(o => o.CreateTime);
           total = query.LongCount();

           if (start > total)
           {
               int tempStart = Int32.Parse(total.ToString()) - limit;
               return query.Skip(tempStart).Take(limit).ToList<Bulletin>();
           }
           return query.Skip(start).Take(limit).ToList<Bulletin>();
       }

    }
}
